package com.xt.website_manager.controller;

import com.xt.website_manager.po.Category;
import com.xt.website_manager.po.CategorySecond;
import com.xt.website_manager.po.Product;
import com.xt.website_manager.po.ProductFile;
import com.xt.website_manager.service.CategoryService;
import com.xt.website_manager.service.ProductService;
import com.xt.website_manager.util.MyPageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;


/**
 * @Author: jiangyu
 * @Date: 2019/8/28 21:06
 * @Description:
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private ServletContext applicationContext;

    /**
     * 查询全部产品列表分页
     */
    @RequestMapping("/findAllProducts.html")
    public String findAllProducts(Model model, MyPageInfo<Product> pageInfo) {
        //去空格
        if (pageInfo.getSearchProName() != null) {
            pageInfo.setSearchProName(pageInfo.getSearchProName().trim());
        }
        if ("".equals(pageInfo.getSearchProName())) {
            pageInfo.setSearchProName(null);
        }
        // 查询分页列表数据
        pageInfo = productService.findAllProducts(pageInfo);

        //查询全部二级类目，为列表中"类型"字段提供支持
        List<CategorySecond> categoryseconds = categoryService.getAllCategorysecond();

        // 查询全部一级类目，为搜索框提供支持
        List<Category> categories = categoryService.getCategories();

        //为回显csid提供支持
        if (pageInfo.getSearchCsid() == null) {
            pageInfo.setSearchCsid(-1);
        }
        model.addAttribute("categories", categories);
        model.addAttribute("categoryseconds", categoryseconds);
        model.addAttribute("pageInfo", pageInfo);
        return "products";
    }

    /**
     * 产品详情
     */
    @RequestMapping("/productDetails.html")
    public Product productDetails(Integer pid) {
        return productService.productDetails(pid);
    }

    /**
     * 删除产品
     */
    @RequestMapping("/deleteProduct.html")
    @ResponseBody
    public String deleteProduct(Integer id) {
        String realPath = applicationContext.getRealPath("/images");
        productService.deleteProduct(id, realPath);
        return "ok";
    }

    /**
     * 添加产品
     */
    @RequestMapping("/addProduct.html")
    public String addProduct() {
        return "addProduct";
    }

    /**
     * 执行添加
     */
    @RequestMapping("/doAdd.html")
    public String doAdd(@RequestParam("proMainPicture") MultipartFile proMainPicture, @RequestParam("proFile") MultipartFile[] proFile, Product product)
            throws IllegalStateException, IOException, ParseException {
        //获取项目路径
        String realPath = applicationContext.getRealPath("/images");
        // 添加产品
        productService.uploadPicture(proMainPicture, proFile, product, realPath);
        return "redirect:/product/findAllProducts.html";
    }

    /**
     * 修改商品页面
     */
    @RequestMapping("/updateProduct.html")
    public String updateProduct(Model model, MyPageInfo<Product> pageInfo) {
        //根据pid查询产品详情
        Product product = productService.productDetails(pageInfo.getPid());
        //根据pid查询附件列表
        List<ProductFile> productFiles = productService.findProductFiles(pageInfo.getPid());

        // 查询全部一级类目，为搜索框提供支持
        List<Category> categories = categoryService.getCategories();
        // 查询当前一级类目下的二级类目:CategoryExt
        Category myCategory = categoryService.findCategoryByCsid(product.getCsid());

        model.addAttribute("myCategory", myCategory);
        model.addAttribute("categories", categories);
        model.addAttribute("product", product);
        model.addAttribute("productFiles", productFiles);
        model.addAttribute("pageInfo", pageInfo);
        return "updateProduct";
    }

    /**
     * 执行修改
     */
    @RequestMapping("/doUpdate.html")
    public String doUpdate(Model model, MyPageInfo<Product> pageInfo, @RequestParam("proMainPicture") MultipartFile proMainPicture, @RequestParam("proFile") MultipartFile[] proFile, Product product) {
        //获取项目路径
        String realPath = applicationContext.getRealPath("/images");
        // 添加产品
        productService.updateProduct(proMainPicture, proFile, product, realPath);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        //findAllProducts.html(model,pageInfo);
        return "forward:/product/findAllProducts.html";
    }


    /**
     * 删除产品主图
     */
    @RequestMapping("/delProductPic.html")
    @ResponseBody
    public String delProductPic(Integer pid) {
        //删除主图
        //1.删除本地磁盘的图片
        Product product = productService.productDetails(pid);
        String realName = applicationContext.getRealPath("/images");
        File file = new File(realName + product.getProPicture());
        file.delete();
        //2.删除数据库中的记录
        productService.delProPic(pid);
        return "ok";
    }

    /**
     * 删除产品附件图片
     */
    @RequestMapping("/delProductFile.html")
    @ResponseBody
    public String delProductFile(Integer profileId) {
        //1.根据id查询附件详情
        ProductFile productFile = productService.productFileDetails(profileId);
        //2.删除本地磁盘中的图片
        new File(applicationContext.getRealPath("/images") + productFile.getImage()).delete();
        //3.删除数据库中的记录
        productService.deleteProFile(profileId);
        return "ok";
    }

}
